package com.codetree.strategy.functionalInterface.strategy;

import com.codetree.strategy.common.Constant;
import com.codetree.strategy.functionalInterface.PaymentFunctionStrategy;
import com.codetree.strategy.functionalInterface.domain.PaymentResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.Resource;

import java.util.HashMap;
import java.util.Map;
import java.util.UUID;


/**
 * 微信支付策略
 */
@Slf4j
@Configuration
public class WechatPayConfig {

    @Value("${payment.wechat.mch-id}")
    private String mchId;

    @Value("${payment.wechat.cert-path}")
    private Resource certPath;

    @Bean(Constant.PAYConstant.WECHAT_PAY)
    public PaymentFunctionStrategy wechatPayStrategy() {
        return amount -> {
            // 模拟真实支付宝调用逻辑
            try {
                // 1. 构造请求参数
                Map<String, String> params = new HashMap<>();
                params.put("mchId", mchId);
                params.put("amount", amount.toString());
                log.info("微信支付请求参数:{}", params);

                // 2. 调用微信API（示例伪代码）
                String response = "{\"success\":true,\"transactionId\":\"11111\",\"message\":\"支付成功\"}";

                log.info("支付成功:{}", response);
            } catch (Exception e) {
                throw new RuntimeException("微信系统异常: ", e);
            }
            return new PaymentResult(Boolean.TRUE, UUID.randomUUID().toString(), "微信支付成功");
        };
    }
}
